﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Pooler : MonoBehaviour 
{
	public int numberOfBasicBullets = 40;
	public Stack<GameObject> basicBulletPool = new Stack<GameObject>();
	public GameObject basicBulletPrefab;

	public int numberOfHeavyBullets = 40;
	public Stack<GameObject> heavyBulletPool = new Stack<GameObject>();
	public GameObject heavyBulletPrefab;

	int numberOfDeathGibs = 400;
	public Stack<GameObject> DeathGibPool = new Stack<GameObject>();
	public GameObject deathGibPrefab;

	int numberOfCoins = 80;
	public Stack<GameObject> coinPool = new Stack<GameObject>();
	public GameObject coinPrefab;

	int numberOfHealth = 15;
	public Stack<GameObject> healthPool = new Stack<GameObject>();
	public GameObject healthPrefab;

	int numberOfSheild = 15;
	public Stack<GameObject> sheildPool = new Stack<GameObject>();
	public GameObject sheildPrefab;

	int numberOfAmmo = 15;
	public Stack<GameObject> ammoPool = new Stack<GameObject>();
	public GameObject ammoPrefab;

	// Use this for initialization
	void Start () 
	{
		FillPool (basicBulletPool, basicBulletPrefab, numberOfBasicBullets);
		FillPool (heavyBulletPool, heavyBulletPrefab, numberOfHeavyBullets);
		FillPool (DeathGibPool, deathGibPrefab, numberOfDeathGibs);
		FillPool (coinPool, coinPrefab, numberOfCoins);
		FillPool (healthPool, healthPrefab, numberOfHealth);
		FillPool (sheildPool, sheildPrefab, numberOfSheild);
		FillPool (ammoPool, ammoPrefab, numberOfAmmo);
	}
	
	void FillPool(Stack<GameObject> list, GameObject prefab, int number)
	{
		Pooler thisPool = GetComponent<Pooler> ();

		for(int i = 0; i< number; i++)
		{
			GameObject instance = Instantiate(prefab) as GameObject;
			instance.GetComponent<Poolable>().SetPooler(thisPool, list);
			AddToPool(instance, list);
		}
	}

	public void AddToPool(GameObject obj, Stack<GameObject> pool)
	{
		pool.Push (obj);
		obj.SetActive(false);
	}

	public GameObject GetFromPool (Stack<GameObject> pool)
	{
		if(pool.Count >0)
		{
			GameObject obj = pool.Pop();
			obj.SetActive(true);
			return obj;
		}
		else
			return null;
	}
}
